package hebClustering.searchEngines;

import java.util.HashSet;
import java.util.Set;

import com.googleapis.ajax.common.PagedList;
import com.googleapis.ajax.common.PagedList.Page;
import com.googleapis.ajax.schema.WebResult;
import com.googleapis.ajax.services.GoogleSearchQueryFactory;
import com.googleapis.ajax.services.WebSearchQuery;
import com.googleapis.ajax.services.enumeration.LanguageFilter;
import com.googleapis.ajax.services.enumeration.ResultSetSize;

/**
 * Google search engine.
 * 
 * @deprecated
 */
public class GoogleSearch implements ISearchEngine {

	@Override
	public Set<WebsiteInformation> search(String query, int numOfResults) {

		Set<WebsiteInformation> urls = new HashSet<WebsiteInformation>();
		
		GoogleSearchQueryFactory factory = GoogleSearchQueryFactory.newInstance("heb-clustering");
		WebSearchQuery webQuery = factory.newWebSearchQuery();
		webQuery.withResultSetSize(ResultSetSize.LARGE);
		webQuery.withLanguage(LanguageFilter.Hebrew);
		webQuery.withQuery(query);
		PagedList<WebResult> response = webQuery.list();
		
		PagedList<WebResult> pageResults = response;
		//long loops = Math.min(response.getPages().size(),numOfResults/8);
		System.out.println();
		
		for (Page p : response.getPages())
		{	
			if (pageResults.getCurrentPageIndex() != 1) 
				pageResults = webQuery.withStartIndex(p.getStart()).list();
			
			System.out.println("results from page " + p.getStart() + ":");
			for (WebResult res : pageResults){
				System.out.println("\t"+res.getUrl());
				WebsiteInformation info = new WebsiteInformation(res.getUrl());
				urls.add(info);
			}
		}		
		
		return urls;
		
	}
	
	
}